//
//  AESBaseCryptor.m
//  ＷebServiceDemo
//
//  Created by kinggril on 2018/9/9.
//  Copyright © 2018年 wulanzhou. All rights reserved.
//

#import "AESBaseCryptor.h"
#import "AESCryptor.h"



@implementation AESBaseCryptor

+(NSData *) encryptWithKey:(NSString *)key data:(NSDictionary *)data{

     NSData *bodyData= [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil];
    
    //AES加密
    NSData *encryptData = [AESCryptor AES128EncryptWithKey:[key dataUsingEncoding:NSUTF8StringEncoding] data:bodyData];
    
    //base64编码
    NSString *encryptStr = [encryptData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    return [encryptStr dataUsingEncoding:NSUTF8StringEncoding];
}

+(NSString *)decryptWithKey:(NSString *)key data:(NSData *)data{

    NSData *decodeData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
    
    //AES解密
    NSData *decryptData = [AESCryptor AES128DecryptWithKey:[key dataUsingEncoding:NSUTF8StringEncoding] data:decodeData];
    
    //转UTF8字符串
    NSString *decryptStr = [[NSString alloc] initWithData:decryptData encoding:NSUTF8StringEncoding];
    
    return decryptStr;
     //NSData *decodeData = [[NSData alloc] initWithBase64EncodedString:self.encryptStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
}

@end
